#include "blockQueue.hpp"

int tasknum = 1;
pthread_mutex_t M;

void *prodcut(void *arg)
{
    while (1)
    {
        taskQueue<std::string> *taskqueue = static_cast<taskQueue<std::string> *>(arg);

        std::string task = "任务";
        pthread_mutex_lock(&M);
        task += std::to_string(tasknum++);
        pthread_mutex_unlock(&M);

        taskqueue->push(task);
        std::cout << task << "进入生产" << std::endl;

        sleep(1);
    }
}
void *consum(void *arg)
{

    while (1)
    {
        taskQueue<std::string> *taskqueue = static_cast<taskQueue<std::string> *>(arg);
        std::string task;
        taskqueue->take_top(task);

        std::cout << task << "被消费，已完成任务" << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_mutex_init(&M, nullptr);
    pthread_t producerA, producerB, consumerA, consumerB;
    taskQueue<std::string> taskq;

    pthread_create(&producerA, nullptr, prodcut, &taskq);
    pthread_create(&producerB, nullptr, prodcut, &taskq);
    pthread_create(&consumerA, nullptr, consum, &taskq);
    pthread_create(&consumerB, nullptr, consum, &taskq);

    pthread_join(producerA, nullptr);
    pthread_join(producerB, nullptr);
    pthread_join(consumerA, nullptr);
    pthread_join(consumerB, nullptr);

    return 0;
}